/**
 * Copyright 2009 by Christian Scheid. This file is part of the switch eclipse plugin and is licensed under the
 * GPL version 3. Please refer to the URL http://www.gnu.org/licenses/gpl-3.0.html for details.
 */
package org.digitalsoul.switchplugin;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * Class ProjectBuilder
 */
public class ProjectBuilder {

    /**
     * 
     */
    protected IProject project;

    /**
     * @param javaProject
     */
    public ProjectBuilder(IProject project) {
        this.project = project;
    }

    /**
     * @param projectName
     */
    public ProjectBuilder(String projectName) {
        createProject(projectName);
    }

    /**
     * @param folder
     */
    public static void createFolder(IFolder folder) {
        try {
            if (!folder.exists()) {
                folder.create(true, true, null);
            }
        }
        catch (CoreException e) {
            e.printStackTrace();
        }
    }

    public IFolder getFolder(IPath path) {
        return project.getFolder(path);
    }

    /**
     * @param folder
     * @param filename
     * @param fileContent
     * @return
     * @throws CoreException
     */
    public IFile createFile(IContainer folder, String filename, String fileContent) throws CoreException {
        IFile createdFile = folder.getFile(new Path(filename));
        createdFile.create(new ByteArrayInputStream(fileContent.getBytes()), true, null);
        return createdFile;
    }
    
    /**
     * @param folder
     * @param filename
     * @return
     * @throws CoreException
     */
    public IFile createFile(IContainer folder, String filename) throws CoreException {
        return createFile(folder, filename, "");
    }

    /**
     * @param folder
     * @param filename
     * @return
     * @throws CoreException
     */
    public IFile createFile(String filename) throws CoreException {
        return createFile(project, filename, "");
    }
    
    /**
     * @param folderName
     * @return
     * @throws CoreException
     */
    public IFolder createFolder(String folderName) throws CoreException {
        IFolder lastFolder = null;
        String[] folders = new String[] {folderName};
        if (folderName.contains("/")) {
            folders = folderName.split("/"); 
        }

        for (String folder : folders) {
            if (lastFolder != null) {
                lastFolder = createFolder(lastFolder, folder);
            }
            else {
                lastFolder = createFolder(project, folder);
            }
        }
        return lastFolder;
    }

    /**
     * @param parentFolder
     * @param foldername
     * @return
     * @throws CoreException
     */
    public IFolder createFolder(IContainer parentFolder, String foldername) throws CoreException {
        IFolder createdFolder = parentFolder.getFolder(new Path(foldername));
        if (!createdFolder.exists()) {
            createdFolder.create(true, true, null);
        }
        return createdFolder;
    }

    /**
     * @param projectName
     */
    public void createProject(String projectName) {
        try {
            IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
            this.project = workspaceRoot.getProject(projectName);
            if (project.exists()) {
                project.refreshLocal(IResource.DEPTH_INFINITE, null);
            }
            else {
                project.create(null);
            }
            if (!project.isOpen()) {
                project.open(null);
            }
        }
        catch (CoreException e) {
            e.printStackTrace();
        }
    }

    /**
     * 
     */
    public void deleteProject() {
        try {
            project.delete(true, true, null);
        }
        catch (CoreException e) {
            e.printStackTrace();
        }
    }

    /**
     * @param file
     * @return
     */
    public static String readFileContents(File file) {
        StringBuffer buffer = new StringBuffer();
        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));

            String line = null;
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }
            reader.close();
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return buffer.toString();
    }
}
